package com.woniu.controller;

import com.woniu.constant.HttpStatus;
import com.woniu.entity.Result;
import com.woniu.entity.dto.InspectorLoginDTO;
import com.woniu.pojo.Inspector;
import com.woniu.service.InspectorService;
import com.woniu.utils.MobileUtil;
import com.woniu.utils.SMSUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("inspectorLogin")
public class LoginController {
    @Resource
    private InspectorService inspectorService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @ApiOperation("巡检端发送短信验证码")
    @GetMapping("/sendMsg/{phone}")
    public Result sendMsg(@PathVariable String phone){

        // 1. 手机号码格式验证
        if(!MobileUtil.checkPhone(phone)){
            return new Result(HttpStatus.ERROR, "手机号码格式错误");
        }

        // 2. 手机号码逻辑校验
        // 2.1. 规定每日可发送次数不超过10次
        String verificationCodeCount = stringRedisTemplate.opsForValue().get("inspector_verification_code" + phone + "_count");
        if("10".equalsIgnoreCase(verificationCodeCount)){
            return new Result(HttpStatus.ERROR, "您今日发送短信验证码条数过多，请明日再试！");
        }
        // 2.2 规定验证码的有效时间，避免有效期内重复发送短信验证码
        String verificationCode = stringRedisTemplate.opsForValue().get("inspector_verification_code" + phone);
            if(!StringUtils.isEmpty(verificationCode)){
                return new Result(HttpStatus.ERROR, "验证码已发出，请勿重新发送");
            }

        // 3. 发送短信验证码
        // 3.1. 生成验证码
        String code = SMSUtils.getRandomNum(4);
        // 3.2. 发送验证码
        System.out.println("【巡检端：登录短信验证码】========================" + code +"========================");
        //SMSUtils.sendSms(phone, code);
        // 3.3. 保存验证码
            // 3.3.1. 设置验证码过期时间
        stringRedisTemplate.opsForValue().set("inspector_verification_code" + phone,code,300,TimeUnit.SECONDS);

        // 3.3.2. 设置验证码发送次数
        if(StringUtils.isEmpty(verificationCode)){
            stringRedisTemplate.opsForValue().set("inspector_verification_code" + phone + "_count", "1");
        }else{
            stringRedisTemplate.opsForValue().set("inspector_verification_code" + phone + "_count", Integer.parseInt(verificationCode) + 1 + "");
        }

        return new Result(HttpStatus.SUCCESS,"验证码发送成功！");
    }

    /**
     * 登录验证
     * @param inspectorLoginDTO
     * @return
     */
    @ApiOperation("巡检端短信验证码登录")
    @PostMapping("/login")
    public Result login(@RequestBody InspectorLoginDTO inspectorLoginDTO){
        // 1. 验证登录信息
        // 1.0 检查验证码是否已发送/已输入
        if(inspectorLoginDTO.getCode().isEmpty()){
            return new Result(HttpStatus.ERROR,"请输入验证码！");
        }
        // 1.1 从redis中获取短信验证码
        String verificationCode = stringRedisTemplate.opsForValue().get("inspector_verification_code"+ inspectorLoginDTO.getPhone());
        // 1.1.1 检验短信验证码是否已过期
            if(verificationCode == null){
                return new Result(HttpStatus.ERROR,"验证码已过期，请重新发送！");
            }
            // 1.1.2 校验短信验证码是否正确
            if(!verificationCode.equalsIgnoreCase(inspectorLoginDTO.getCode())){
                return new Result(HttpStatus.ERROR,"验证码错误，请重新输入！");
            }
        // 1.2 验证手机号码是否存在于数据库
        Inspector inspector = inspectorService.getByPhone(inspectorLoginDTO.getPhone());
        if(inspector == null){
            return new Result(500, "该手机号码没有注册!");
        }
        //2. 清空redis短信验证码
        stringRedisTemplate.delete("inspector_verification_code" + inspectorLoginDTO.getPhone());
        stringRedisTemplate.delete("inspector_verification_code" + inspectorLoginDTO.getPhone() + "_count");

        return new Result(HttpStatus.SUCCESS,"巡检员登录成功！");
    }

}
